package servlets;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sessions.HelpManagerRemote;
import entities.HelpRequest;

/**
 * Servlet implementation class HelpReqs
 */
public class HelpReqs extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HelpReqs() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		try {
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("HelpManager/remote");
			HelpManagerRemote r = (HelpManagerRemote) ref;
			List<HelpRequest> incoming = r.fetchIncoming(username);
			List<HelpRequest> outgoing = r.fetchOutgoing(username);
			List<HelpRequest> withFeedback = r.fetchRequestWithFeedback();
			session.setAttribute("incoming", incoming);
			session.setAttribute("outgoing", outgoing);
			session.setAttribute("withFeedback", withFeedback);
			response.sendRedirect("helpReqs.jsp");
			return;
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}

}
